package com.lh.runmanage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.runmanage.model.InPutParam.LogByPageModel;
import com.lh.runmanage.model.InPutParam.LogInParam;
import com.lh.runmanage.model.LogModel;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.model.ResultVOPage;
import com.lh.runmanage.service.LogService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-12 15:59
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/logController")
@Api(value = "项目控制层", description = "系统运行态势平台管理端日志方法")
public class LogController {
    @Autowired
    LogService logService;
    @Autowired
    GetPropertiesClass getPropertiesClass;

    private int deleteLogByCreateDate() {
        LogInParam logInParam = new LogInParam();
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DAY_OF_MONTH, -6); //把日期往后增加一天,整数  往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        logInParam.setCreateTime(date);
        int updateCount = logService.deleteLogByCreateDate(logInParam);
        return updateCount;
    }


    /**
     * 分页系统日志，方法ID：SE2021071215564761065C718B7856D
     *
     * @param userName 表sys_log,字段名userName:用户名称
     * @param page     当前页数
     * @param limit    每页条数
     * @return 分页数据
     */
    @ApiOperation(value = "分页系统日志[分页数据],方法ID：SE2021071215564761065C718B7856D", notes = "/logController/V1/selectLogByPage?userName=userName&page=page&limit=limit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名称", dataType = "String")
            , @ApiImplicitParam(name = "beginData", value = "开始日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endData", value = "结束日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/V1/selectLogByPage")
    public ResultVOPage selectLogByPage(@RequestParam(value = "userName", required = false) String userName
            , @RequestParam(value = "beginData") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginData
            , @RequestParam(value = "endData") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endData
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) throws UnsupportedEncodingException {
        if (userName != null) {
            userName = userName.trim();
            if (userName.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "userName"), ResultVOPage.class, null);
            }
        }
        LogByPageModel logByPageModel = new LogByPageModel();
        logByPageModel.setUserName(userName);
        logByPageModel.setBeginDate(beginData);
        logByPageModel.setEndDate(endData);
        PageHelper.startPage(page, limit);
        List<LogModel> logModels = logService.selectLogByPage(logByPageModel);
        PageInfo pageInfo = new PageInfo<>(logModels, limit);
        deleteLogByCreateDate();
        return ResultStruct.successPage(logModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }
}